package com.album.web.domain;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @version v1.0
 * @ProjectName: album
 * @ClassName: DataUtil
 * @Description: TODO(一句话描述该类的功能)
 * @Author: xt
 * @Date: 2020/4/22 17:14
 */
public class DataUtil {
    public static void main(String[] args) {
        rua("com.album.web.domain.AlbumBean");
    }

    public static void rua(String path) {
        try {
            //获取表实体类
            Class clazz = Class.forName(path);

            //获取表实体类的字段
            Field[] fields = clazz.getDeclaredFields();

            //用以记录主键字段
            List<String> idList = new ArrayList<>();
            List<String> columnList = new ArrayList<>();
            StringBuilder createStr = new StringBuilder(1000);

            //获取表名
            Table tableAnnotation = (Table) clazz.getAnnotation(Table.class);
            createStr.append("create table " + tableAnnotation.name() + "(");
            for (Field field : fields) {
                //获取每个字段所有注解
                Annotation[] annotations = field.getDeclaredAnnotations();
                Boolean isId = false;
                for (Annotation annotation : annotations) {
                    if (annotation instanceof Id) {
                        isId = true;
                    } else if (annotation instanceof Column) {
                        Column column = (Column) annotation;
                        if (true == isId) {
                            idList.add(column.name());
                        }
                        String columnStr = "";

                        Class<?> type = field.getType();
                        Boolean nullable = column.nullable();
                        String nullableStr = "not null";
                        if (nullable) nullableStr = "";
                        //根据字段类型以及配置，转换为相应的mysql类型
                        String typeStr = "";
                        if (Integer.class == type) {
                            typeStr = "int";
                        } else if (String.class == type) {
                            int precision = column.precision();
                            if (0 == precision) {
                                precision = 64;
                            }
                            typeStr = "varchar(" + precision + ")";
                        } else if (BigDecimal.class == type) {
                            int precision = column.precision();
                            int scale = column.scale();
                            typeStr = "decimal(" + precision + "," + scale + ")";
                        } else {
                            break;
                        }
                        columnStr +=
                                column.name() + " "
                                        + typeStr + " "
                                        + nullableStr;
                        columnList.add(columnStr);
                        break;
                    }
                }
            }
            createStr.append(columnList.stream().collect(Collectors.joining(",")) + ");");
            if (0 != idList.size()) {
                //主键
                String primaryKey = "alter table  "
                        + tableAnnotation.name()
                        + " add primary key ("
                        + idList.stream().collect(Collectors.joining(","))
                        + ");";
                createStr.append(primaryKey);
            }
            System.out.println(createStr);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
